package haizhi.fruitmall.service.impl;

import haizhi.fruitmall.dao.mapper.AuthMapper;
import haizhi.fruitmall.pojo.po.Auth;
import haizhi.fruitmall.pojo.po.AuthExample;
import haizhi.fruitmall.service.AuthService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {

	@Autowired
	private AuthMapper authMapper;

	@Override
	public void addAuth(Auth auth) {
		authMapper.insertSelective(auth);
	}

	@Override
	public void deleteAuthA(String auth_c, String auth_a) {
		AuthExample authExample = new AuthExample();
		AuthExample.Criteria criteria = authExample.createCriteria();
		criteria.andAuthCEqualTo(auth_c);
		criteria.andAuthAEqualTo(auth_a);
		authMapper.deleteByExample(authExample);
	}

	@Override
	public void deleteAuthC(String auth_c) {
		AuthExample authExample = new AuthExample();
		AuthExample.Criteria criteria = authExample.createCriteria();
		criteria.andAuthAEqualTo(auth_c);
		authMapper.deleteByExample(authExample);
	}

	@Override
	public void updateAuthAName(Auth auth, String auth_c, String auth_a) {
		AuthExample authExample = new AuthExample();
		AuthExample.Criteria criteria = authExample.createCriteria();
		criteria.andAuthCEqualTo(auth_c);
		criteria.andAuthAEqualTo(auth_a);
		authMapper.updateByExampleSelective(auth, authExample);
	}

	@Override
	public void updateAuthCName(Auth auth, String auth_c) {
		AuthExample authExample = new AuthExample();
		AuthExample.Criteria criteria = authExample.createCriteria();
		criteria.andAuthAEqualTo(auth_c);
		authMapper.updateByExampleSelective(auth, authExample);
	}

	@Override
	public Map<String, List<String>> getAllAuth() {
		Map<String, List<String>> authMap = new HashMap<String, List<String>>();
		AuthExample authExample = new AuthExample();
		AuthExample.Criteria criteria = authExample.createCriteria();
		criteria.andAuthIdIsNotNull();
		List<Auth> auths = authMapper.selectByExample(authExample);
		for (Auth auth : auths) {
			if (auth.getAuthPid() == null
					&& authMap.get(auth.getAuthC()) == null) {
				List<String> authAs = new ArrayList<String>();
				authMap.put(auth.getAuthC(), authAs);
			} else {
				if (authMap.get(auth.getAuthC()) == null) {
					List<String> authAs = new ArrayList<String>();
					authAs.add(auth.getAuthA());
					authMap.put(auth.getAuthC(), authAs);
				} else {
					authMap.get(auth.getAuthC()).add(auth.getAuthA());
				}
			}
		}
		return authMap;
	}

	@Override
	public List<Auth> getAuthsByAuthA(String auth_c, String auth_a) {
		AuthExample authExample = new AuthExample();
		AuthExample.Criteria criteria = authExample.createCriteria();
		criteria.andAuthCEqualTo(auth_c).andAuthAEqualTo(auth_a);
		return authMapper.selectByExample(authExample);
	}

	@Override
	public List<Auth> getAuthsByAuthC(String auth_c) {
		AuthExample authExample = new AuthExample();
		AuthExample.Criteria criteria = authExample.createCriteria();
		criteria.andAuthAEqualTo(auth_c).andAuthPidIsNull();
		return authMapper.selectByExample(authExample);
	}

	@Override
	public Auth getAuth(Integer auth_id) {
		return authMapper.selectByPrimaryKey(auth_id);
	}
}
